/**
 * 功能描述 列表
 */

define(function (require, exports, module) {
	var Component = require("grid");
	var Dialog = require("dialog");
	var proChangeRoom = require("../constant/fetchProperties");
	var aw = require("ajaxwrapper");
	var Form = require("form");
    var instance = {
        initComponent : function (params, widget) {
            var component = new Component({
            	parentNode : ".J-grid",	
            	url : "api/changeroom/querygroup",
            	autoRender : false,
				params : function(){
					var subnav = widget.get("subnav");
					var time = subnav.getValue("time");
					var obj = {
                			"contactName" : subnav.getValue("search"),
                			"servicePoint" : subnav.getValue("servicePoint"),
                			"destination" : subnav.getValue("destination"),
                			"startDate": time.start,
							"endDate": time.end,
							fetchProperties : proChangeRoom.fetchProperties
                	};
	    			return obj;
				},
				model : {
				 allowEdit: false,
				 columns  : [{
					 name : "dname",
					 label : "目的地"
				 },{
					 name : "startDate",
					 label : "开始日期",
					 format : "date",
				 },{
					 name : "endDate",
					 label : "结束日期",
					 format : "date"
				 },{
					 name : "groupType",
					 label : "组团类型",
					 format : function(value,row){
//						 TravelGroup("旅行团"), Individual("个人"), IndivMember("会员散客");
//						 GroupReserve("组团"), NonMember("散客"), VisitDoorFIT("上门散客");
						 //散客
						 if(value=="TravelGroup"){
							 return "旅行团";
						 }else if(value=="Individual"){
							 return "个人";
						 }else if(value=="IndivMember"){
							 return "会员散客";
						 //组团
						 }else if(value=="GroupReserve"){
							 return "组团";
						 }else if(value=="NonMember"){
							 return "散客";
						 }else if(value=="VisitDoorFIT"){
							 return "上门散客";
						 }else {
							 return value;
						 }
					 }
				 },{
					 name : "groupUnit",
					 label : "组团单位",
				 },{
					 name : "contactName",
					 label : "联系人",
					 format : function(value,row){
						 if(value=="团体无联系人"){
							 return "";
						 }else{
							 return value;
						 }
					 }
				 },{
					 name : "changeRoomDate",
					 label : "换房日期",
					 format : "date"
				 },{
						name:"show",
						label:"操作",
						format:"button",
						formatparams:[{
							id:"edit",
							icon:"icon-edit",
							text:"换房",
							handler:function(index,data,rowEle){
								//data是执行url得来的数据，不在列中显示的也会有
	        					widget.get("form").reset();
//	        					widget.get("pergrid").setData([]);
	        					widget.show([".J-form",".J-editgrid"]).hide([".J-grid"]);
								widget.get("subnav").hide(["search", "servicePoint", "destination", "time"]).show(["save", "return"]);
								widget.get("form").setValue("dname",data.dname);
								widget.get("form").setValue("startDate",data.startDate);
								widget.get("form").setValue("endDate",data.endDate);
								widget.get("form").setValue("groupUnit",data.groupUnit);
								widget.get("form").setValue("groupType",data.groupType);
								widget.get("form").setValue("pkCIImplement",data.pkCIImplement);
								
//								widget.get("editgrid").show();
								
								var subnav = widget.get("subnav");
                	        	var rangeDate = subnav.getValue("time");
                	        	var servicePoint = subnav.getValue("servicePoint");
                	        	var destination = subnav.getValue("destination");
                	        	subnav.setValue("search", data.contactName);
                	        	
								widget.get("editgrid").refresh({
								 "pkCIImplement" : data.pkCIImplement,
								 "groupUnit" : data.groupUnit,
								 "groupType" : data.groupType,
								 "contactName" : data.contactName,
								 "destination" : destination,
								 "servicePoint" : servicePoint,
                       	         "startDate" : rangeDate.start,
                       	         "endDate" : rangeDate.end,
                       	          fetchProperties:"*,croomType.name,croom.code"
                    			 },function(data){
                    				 widget.get("form").setValue("applyNum",data.length);
                    				 widget.get("form").setValue("editStatus","1");
                    			 });
							}
						},{
							id:"cancel",
							icon:"icon-cancel",
							text:"查看",
							handler:function(index,data,rowEle){
								widget.set("detail",true);
								//data是执行url得来的数据，不在列中显示的也会有
	        					widget.get("form").reset();
//	        					widget.get("pergrid").setData([]);
	        					widget.show([".J-form",".J-detailgrid"]).hide([".J-grid"]);
								widget.get("subnav").hide(["search", "servicePoint", "destination", "time"]).show(["return"]);
								widget.get("form").setValue("dname",data.dname);
								widget.get("form").setValue("startDate",data.startDate);
								widget.get("form").setValue("endDate",data.endDate);
								widget.get("form").setValue("groupUnit",data.groupUnit);
								widget.get("form").setValue("groupType",data.groupType);
								widget.get("form").setValue("pkCIImplement",data.pkCIImplement);
//								widget.get("editgrid").show();
								var subnav = widget.get("subnav");
                	        	var rangeDate = subnav.getValue("time");
                	        	var servicePoint = subnav.getValue("servicePoint");
                	        	var destination = subnav.getValue("destination");
                	        	subnav.setValue("search", data.contactName);
								widget.get("detailgrid").refresh({
								 "pkCIImplement" : data.pkCIImplement,
								 "groupUnit" : data.groupUnit,
								 "groupType" : data.groupType,
								 "contactName" : data.contactName,
								 "destination" : destination,
								 "servicePoint" : servicePoint,
                       	         "startDate" : rangeDate.start,
                       	         "endDate" : rangeDate.end,
                       	          fetchProperties:"*,croomType.name,croom.code"
                    			 },function(data){
                    				 widget.get("form").setValue("applyNum",data.length);
                    				 widget.get("form").setValue("editStatus","0");
                    			 });
							
							}
						}]
					}]
				}
            });
            widget.set("grid", component);
        }
    };
    module.exports = instance;
});
